//
//  IWNewFeatureCtrl.m
//  WeiBo17
//
//  Created by teacher on 15/8/17.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "IWNewFeatureCtrl.h"
#import "CZMainTabBarVC.h"

@interface IWNewFeatureCtrl ()<UIScrollViewDelegate>


@property (nonatomic, weak) UIPageControl *pageControl;


@end

@implementation IWNewFeatureCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    //初始化一个ScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    //隐藏水平方向的滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    //开启分页
    scrollView.pagingEnabled = YES;
    
    //监听滑动-->成为代理
    scrollView.delegate = self;
    
    
    NSInteger count = 4;
    
    for (int i=0; i<count; i++) {
        
        //循环添加imageView
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"new_feature_%d",i + 1]];
        //设置大小与位置
        imageView.size = scrollView.size;
        imageView.x = i * scrollView.width;
        
        [scrollView addSubview:imageView];
        
        
        if (i == count-1) {
            //最后一页
            [self setupLastPageWithView:imageView];
        }
    }
    
    //设置scrollView的内容大小
    [scrollView setContentSize:CGSizeMake(count * scrollView.width, 0)];
    
    [self.view addSubview:scrollView];
    
    
    //添加pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    //设置显示几页
    pageControl.numberOfPages = count;
    //设置选中的颜色与默认的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
    //设置位置
    pageControl.centerX = self.view.width * 0.5;
    pageControl.y = self.view.height - 100;
    
    //沿着边切掉多余的部分
//    pageControl.clipsToBounds = YES;
    
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    
}

- (void)setupLastPageWithView:(UIImageView *)imageView{
    
    imageView.userInteractionEnabled = true;
    
    UIButton *enterButton = [[UIButton alloc] init];
    //设置不同状态的背景图片
    [enterButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [enterButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    //设置大小
    enterButton.size = enterButton.currentBackgroundImage.size;
    //设置位置
    enterButton.centerX = imageView.width * 0.5;
    enterButton.y = imageView.height - 150;
    
    //设置文字
    [enterButton setTitle:@"进入微博" forState:UIControlStateNormal];
    
    //监听进入微博按钮点击
    [enterButton addTarget:self action:@selector(enterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [imageView addSubview:enterButton];
    
    
    UIButton *shareBtn = [[UIButton alloc] init];
    [shareBtn setTitle:@"分享到微博" forState:UIControlStateNormal];
    //设置不同状态的图片
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //自适应内容大小
    [shareBtn sizeToFit];
    
    shareBtn.centerX = enterButton.centerX;
    shareBtn.y = enterButton.y - 40;
    
    
    [imageView addSubview:shareBtn];
    
}

//分享的按钮点击
- (void)shareBtnClick:(UIButton *)button{
    button.selected = !button.selected;
}

//进入微博按钮点击
- (void)enterBtnClick:(UIButton *)button{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [CZMainTabBarVC new];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //计算滑动到第几页
    double page = scrollView.contentOffset.x / scrollView.width;
    
//    NSLog(@"%d",page);
    
    self.pageControl.currentPage = (int)(page + 0.5);
    
}

@end
